class Reply < ActiveRecord::Base
	# include Elasticsearch::Model
	include Searchable
	belongs_to :user,counter_cache: true
	belongs_to :topic,counter_cache: true
	has_many :notifications,dependent: :destroy
	before_save :generate_body_html
	after_create :send_notifications





	private
		def generate_body_html
			self.body_html=MyMarkDown::Markdowns.converter(self.body)
		end

		def here_users
			all=[]
			self.topic.replies.each do |c|
				all << c.user
			end
			all << self.topic.user
			all.uniq
		end

		def send_notifications
			here_users.each do |m|
				Notification.create(user_id: m.id,reply_id: self.id) unless m.id == self.user_id
			end

		end


end
Reply.import force: true
